/*=============================================================================
#    The element-comm is a tentative program used to test our ideas.           
#                       Copyright (C)  bgao                                    
#
#     This program is free software; you can redistribute it and/or            
#      modify it under the terms of the GNU General Public License             
#     as published by the Free Software Foundation; either version 2           
#         of the License, or (at your option) any later version.               
#
#     This program is distributed in the hope that it will be useful,          
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            
#             GNU General Public License for more details.                    
#
#    You should have received a copy of the GNU General Public License        
#     along with this program; if not, write to the Free Software             
#      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,             
#                       MA  02110-1301, USA.                                  
#==============================================================================
# $Id$ 
#
# $Author$ 
#
# $Revision$ 
#
# $Date$ 
#
# Description: 
#
=============================================================================*/
#include <avr/io.h>
#include "wsn-platform.h"
#include "cc2420.h"
#include "rtos-wsn.h"

#ifdef _Debug_zigbee_

#include "zigbee.def"

extern BYTE	RxBuffer[RX_BUFFER_SIZE];
extern BYTE RxRead;
extern BYTE	RxWrite;
#endif



void cc2420_io_init(void)
{

	/* PA6=RESETn  PA5 =VREG_EN*/
	DDRA |= BIT_SET(RESET_N) | BIT_SET(VREG_EN);    								  

	/* PB7 link to FIFO of CC2420 */
	DDRB &= ~BIT_SET(FIFO);    
	DDRB &= ~BIT_SET(SO);
	DDRB |= BIT_SET(CSN) | BIT_SET(SCK) | BIT_SET(SI); 	

	DDRD &= ~(BIT_SET(SFD) | BIT_SET(CCA));  
	DDRE &= ~BIT_SET(FIFOP);

	/*INT setup*/
	cc2420_disable_fifop();//Disable interrupt INT6
	EICRB |= BIT_SET(ISC61);//set falling edge trigger
	EICRB &= ~BIT_SET(ISC60);

}


/* CC2420 FIFOP ISR */
void isr_cc2420_fifop(void)
{
	int count = 0;
	wsn_LeaveCritical();
#ifdef _Debug_zigbee_	
	if (CC2420_RX_IS_OVERFLOW()) {
		cc2420_flush_Rxfifo();
		RxWrite = 0;
		return;
	}
	while (CC2420_FIFO_IS_ACTIVE()/* Data in FIFO */) {
		count = cc2420_read_fifo(&RxBuffer[RxWrite], RX_BUFFER_SIZE - RxWrite);
		RxWrite += count;
		if (RxWrite >= RX_BUFFER_SIZE) /* Data error */
			RxWrite = 0;
	}
#endif
}

void cc2420_enable_fifop(void)
{
	EIMSK |= BIT_SET(FIFOP);
}

void cc2420_disable_fifop(void)
{
	EIMSK &= ~BIT_SET(FIFOP);
}	


